中国科技核心期刊

中文核心期刊

CSCD来源期刊

空间控制技术与应用 ›› 2025, Vol. 51 ›› Issue (1): 115-122.doi: 10.3969/j.issn.1674 1579.2025.01.012

• 论文与报告 • 上一篇    

基于大语言模型的C程序单元测试方法

  

  1. 北京交通大学
  • 出版日期:2025-02-26 发布日期:2025-03-07
  • 基金资助:
    国家自然科学基金资助项目(62202040)

Unit Test Generation for C Programs via Large Language Models

  • Online:2025-02-26 Published:2025-03-07

摘要: 单元测试用于检验软件单一模块的功能是否正确,是软件开发过程中的重要步骤,可以及时发现代码中的缺陷,提升软件的质量和可信度. 由于手动编写单元测试费时费力,经常遗漏覆盖重要的代码逻辑.为此,研究者提出单元测试用例自动生成技术. 近来,预训练大语言模型( large language models, LLM) 已经广泛应用于代码生成相关任务. 然而, 当前在重要的系统级编程语言C上, 还没有相关工作. 为了填补这一空白, 本文面向C程序设计并实现了基于LLM 的单元测试用例生成方法LLM4CUTCG.该方法结合LLM多智能体交互和程序分析技术,客服了LLM内在问题. 为了验证方法效果,收集了125 个C 语言目标程序,并针对这些程序生成测试用例. 实验结果表明, LLM4CUTCG 生成的测试行覆盖率为91. 71% ,测试预言正确率为50. 05% . 其覆盖率优于传统方法符号执行.

关键词: 软件测试, 单元测试, 测试自动生成, 代码自动生成, 大语言模型, 测试预言

Abstract: Unit testing is used to verify the functionality of individual software modules, making it a crucial step in the software development process. It helps in identifying defects in the code in a timely manner, thereby improving the quality and reliability of the software. Manually writing unit tests is time consuming and labor intensive, often missing important code logic. To address this, researchers have proposed techniques for automatic generation of unit tests. Recently, pre trained large language models (LLM) have been widely applied to tasks related to code generation, and there has been work on generating unit tests for languages such as Java and JavaScript. However, in safety critical domains such as aerospace, C language programs are predominantly used. To fill this gap, we designed and implemented an LLM based multi agent interaction method for generating unit tests for C programs, named LLM4CUTCG. To verify the effectiveness of this method, we constructed 125 target programs without data leakage risks and generated tests for these programs. Experimental results show that the line coverage of the tests generated by LLM4CUTCG is 91.71%, and the assertion correctness rate is 50.05%. The coverage rate outperforms traditional methods such as symbolic execution and fuzz testing.

Key words: software testing, unit testing, test generation, code generation, large language model, test oracle

中图分类号: 

  • V44